<?php

namespace App\Http\Controllers\Api\Admin;

use App\Exceptions\ConditionException;
use App\Http\Requests\Admin\MenusRequest;
use App\Http\Resources\Admin\MenusResource;
use App\Repositories\Admin\MenusRepository;
use App\Services\MenusService;
use App\Services\RoleService;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;

/**
 * Class MenusController
 * @package App\Http\Controllers\Api\Admin\User
 */
class MenusController extends ApiController
{

    /**
     * @var MenusRepository
     */
    private $repo;

    /**
     * @var MenusService
     */
    private $service;

    /**
     * MenusController constructor.
     * @param MenusRepository $repo
     * @param MenusService $service
     */
    public function __construct(MenusRepository $repo, MenusService $service)
    {
        $this->repo = $repo;
        $this->service = $service;
        $this->middleware('jwt:admin');
    }

    /**
     * 获取列表信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $this->hasPermission('menu-main');
        $map = $request->all();
        $menus = $this->service->seek(array_merge($map, ['parent_id' => 0]));
        return $this->success(MenusResource::collection($menus));
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getChecked(Request $request)
    {
        $this->hasPermission('menu-man');
        $role = $request->get('role_id') ? app(RoleService::class)->find($request->get('role_id')) : null;
        $map = $request->only(['parent_id', 'name']);
        $list = $this->repo->handleMenus($map, $role);
        return $this->success($list);
    }

    /**
     * 获取详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id)
    {
        $this->hasPermission('menu-main');
        $info = $this->service->find($id);
        return $this->success(new MenusResource($info));
    }


    /**
     * 创建
     * @param MenusRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(MenusRequest $request)
    {
        $this->hasPermission('menu-opera');
        $info = $this->service->create($request->all());
        return $this->success(new MenusResource($info));
    }

    /**
     * 修改
     * @param $id
     * @param MenusRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update($id, MenusRequest $request)
    {
        $this->hasPermission('menu-opera');
        $data = $request->all();
        $info = $this->service->find($id);
        // 获取上级
        if (isset($data['parent_id']) && $data['parent_id'] != 0 && $info->children()->count() > 0) {
            throw new ConditionException('该目录仍有下级目录存在，请先处理下级目录再进行操作！');
        }
        $info->fill($data)->save();
        return $this->success(new MenusResource($info));
    }

    /**
     * 删除记录
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        $this->hasPermission('menu-del');
        $info = $this->service->find($id);
        // 获取上级
        if ($info->children()->count() > 0) {
            throw new ConditionException('该目录仍有下级目录存在，请先处理下级目录再进行操作！');
        }
        $this->service->delete($id);
        return $this->message('删除成功！');
    }
}
